package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.TurntableLotteryRecord;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

public class TurntableRecordDaoImpl extends TempDataObjectDao<TurntableLotteryRecord> {

    public LinkedList<TurntableLotteryRecord> getTurntableLotteryRecordList(int maxCount) {
        ResultSet rs = null;
        CallableStatement call = null;
        LinkedList<TurntableLotteryRecord> dataList = new LinkedList<>();
        String sql = "select * from t_u_turntable_lottery_record order by recordTime desc limit " + maxCount;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                TurntableLotteryRecord data = parse(rs);
                dataList.addLast(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    @Override
    protected boolean addData(TurntableLotteryRecord data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_turntable_lottery_record(recordUid,userId,nickName,goodsId,count,recordTime) values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, data.getRecordUid());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getNickName());
            call.setInt(i++, data.getGoodsId());
            call.setInt(i++, data.getCount());
            call.setLong(i++, data.getRecordTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(TurntableLotteryRecord data) {
        PreparedStatement call = null;
        String sql = "update t_u_turntable_lottery_record set userId=?,nickName=?,goodsId=?,count=?,recordTime=? where recordUid=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getNickName());
            call.setInt(i++, data.getGoodsId());
            call.setInt(i++, data.getCount());
            call.setLong(i++, data.getRecordTime());
            call.setString(i++, data.getRecordUid());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected TurntableLotteryRecord parseData(ResultSet rs) throws SQLException {
        TurntableLotteryRecord data = new TurntableLotteryRecord();
        data.setRecordUid(rs.getString("recordUid"));
        data.setUserId(rs.getLong("userId"));
        data.setNickName(rs.getString("nickName"));
        data.setGoodsId(rs.getInt("goodsId"));
        data.setCount(rs.getInt("count"));
        data.setRecordTime(rs.getLong("recordTime"));
        return data;
    }
}
